1. Project Clover database Sat Apr 27 2024 21:29:13 CDT
  2. Package net.bytebuddy.description.type

File TypeDefinition.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

12
27
13
3
297
99
19
0.7
2.08
4.33
1.46

Classes

Class Line # Actions
TypeDefinition 15 0 - 0 0
-1.0 -
TypeDefinition.Sort 131 18 0% 13 0
1.0100%
TypeDefinition.SuperClassIterator 252 9 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 2920 tests. .

Source view

1    package net.bytebuddy.description.type;
2   
3    import net.bytebuddy.description.NamedElement;
4    import net.bytebuddy.description.field.FieldList;
5    import net.bytebuddy.description.method.MethodList;
6    import net.bytebuddy.implementation.bytecode.StackSize;
7   
8    import java.lang.reflect.*;
9    import java.util.Iterator;
10    import java.util.NoSuchElementException;
11   
12    /**
13    * Implementations define a type, either as a {@link TypeDescription} or as a {@link TypeDescription.Generic}.
14    */
 
15    public interface TypeDefinition extends NamedElement, Iterable<TypeDefinition> {
16   
17    /**
18    * Returns this type definition as a generic type.
19    *
20    * @return This type definition represented as a generic type.
21    */
22    TypeDescription.Generic asGenericType();
23   
24    /**
25    * Returns the erasure of this type. Wildcard types ({@link TypeDescription.Generic.Sort#WILDCARD})
26    * do not have a well-defined erasure and cause an {@link IllegalStateException} to be thrown.
27    *
28    * @return The erasure of this type.
29    */
30    TypeDescription asErasure();
31   
32    /**
33    * Returns the super class of this type. A super type is only defined for non-generic types ({@link Sort#NON_GENERIC}),
34    * parameterized types ({@link Sort#PARAMETERIZED}) or generic array types ({@link Sort#GENERIC_ARRAY}) types. Interface types
35    * and the {@link Object} class do not define a super class where {@code null} is returned. Array types define {@link Object}
36    * as their direct super class.
37    *
38    * @return The super class of this type or {@code null} if no super class exists for this type.
39    */
40    TypeDescription.Generic getSuperClass();
41   
42    /**
43    * Returns the interfaces that this type implements. A super type is only defined for non-generic types ({@link Sort#NON_GENERIC}),
44    * parameterized types ({@link Sort#PARAMETERIZED}) or generic array types ({@link Sort#GENERIC_ARRAY}) types.
45    *
46    * @return The interfaces that this type implements.
47    */
48    TypeList.Generic getInterfaces();
49   
50    /**
51    * Returns the fields that this type declares. A super type is only defined for non-generic types ({@link Sort#NON_GENERIC}),
52    * parameterized types ({@link Sort#PARAMETERIZED}) or generic array types ({@link Sort#GENERIC_ARRAY}) types. Generic array
53    * types never define fields and the returned list is always empty for such types.
54    *
55    * @return The fields that this type declares. A super type is only defined for non-generic types ({@link Sort#NON_GENERIC}),
56    * parameterized types ({@link Sort#PARAMETERIZED}) or generic array types ({@link Sort#GENERIC_ARRAY}) types. Generic array
57    * types never define methods and the returned list is always empty for such types.
58    */
59    FieldList<?> getDeclaredFields();
60   
61    /**
62    * Returns the methods that this type declares.
63    *
64    * @return The methods that this type declares.
65    */
66    MethodList<?> getDeclaredMethods();
67   
68    /**
69    * <p>
70    * Returns the component type of this type.
71    * </p>
72    * <p>
73    * Only non-generic types ({@link TypeDescription.Generic.Sort#NON_GENERIC}) and generic array types
74    * {@link TypeDescription.Generic.Sort#GENERIC_ARRAY}) define a component type. For other
75    * types, an {@link IllegalStateException} is thrown.
76    * </p>
77    *
78    * @return The component type of this type or {@code null} if this type does not represent an array type.
79    */
80    TypeDefinition getComponentType();
81   
82    /**
83    * Returns the sort of the generic type this instance represents.
84    *
85    * @return The sort of the generic type.
86    */
87    Sort getSort();
88   
89    /**
90    * Returns the name of the type. For generic types, this name is their {@link Object#toString()} representations. For a non-generic
91    * type, it is the fully qualified binary name of the type.
92    *
93    * @return The name of this type.
94    */
95    String getTypeName();
96   
97    /**
98    * Returns the size of the type described by this instance. Wildcard types
99    * ({@link TypeDescription.Generic.Sort#WILDCARD} do not have a well-defined a stack size and
100    * cause an {@link IllegalStateException} to be thrown.
101    *
102    * @return The size of the type described by this instance.
103    */
104    StackSize getStackSize();
105   
106    /**
107    * Checks if the type described by this entity is an array.
108    *
109    * @return {@code true} if this type description represents an array.
110    */
111    boolean isArray();
112   
113    /**
114    * Checks if the type described by this entity is a primitive type.
115    *
116    * @return {@code true} if this type description represents a primitive type.
117    */
118    boolean isPrimitive();
119   
120    /**
121    * Checks if the type described by this instance represents {@code type}.
122    *
123    * @param type The type of interest.
124    * @return {@code true} if the type described by this instance represents {@code type}.
125    */
126    boolean represents(Type type);
127   
128    /**
129    * Represents a {@link TypeDescription.Generic}'s form.
130    */
 
131    enum Sort {
132   
133    /**
134    * Represents a non-generic type.
135    */
136    NON_GENERIC,
137   
138    /**
139    * Represents a generic array type.
140    */
141    GENERIC_ARRAY,
142   
143    /**
144    * Represents a parameterized type.
145    */
146    PARAMETERIZED,
147   
148    /**
149    * Represents a wildcard type.
150    */
151    WILDCARD,
152   
153    /**
154    * Represents a type variable that is attached to a {@link net.bytebuddy.description.TypeVariableSource}.
155    */
156    VARIABLE,
157   
158    /**
159    * Represents a type variable that is merely symbolic and is not attached to a {@link net.bytebuddy.description.TypeVariableSource}
160    * and does not defined bounds.
161    */
162    VARIABLE_SYMBOLIC;
163   
164    /**
165    * Describes a loaded generic type as a {@link TypeDescription.Generic}.
166    *
167    * @param type The type to describe.
168    * @return A description of the provided generic type.
169    */
 
170  288432 toggle public static TypeDescription.Generic describe(Type type) {
171  288432 return describe(type, TypeDescription.Generic.AnnotationReader.NoOp.INSTANCE);
172    }
173   
174    /**
175    * Describes the generic type while using the supplied annotation reader for resolving type annotations if this
176    * language feature is available on the current JVM.
177    *
178    * @param type The type to describe.
179    * @param annotationReader The annotation reader for extracting type annotations.
180    * @return A description of the provided generic annotated type.
181    */
 
182  525290 toggle protected static TypeDescription.Generic describe(Type type, TypeDescription.Generic.AnnotationReader annotationReader) {
183  525290 if (type instanceof Class<?>) {
184  500247 return new TypeDescription.Generic.OfNonGenericType.ForLoadedType((Class<?>) type, annotationReader);
185  25043 } else if (type instanceof GenericArrayType) {
186  253 return new TypeDescription.Generic.OfGenericArray.ForLoadedType((GenericArrayType) type, annotationReader);
187  24790 } else if (type instanceof ParameterizedType) {
188  11289 return new TypeDescription.Generic.OfParameterizedType.ForLoadedType((ParameterizedType) type, annotationReader);
189  13501 } else if (type instanceof TypeVariable) {
190  4193 return new TypeDescription.Generic.OfTypeVariable.ForLoadedType((TypeVariable<?>) type, annotationReader);
191  9308 } else if (type instanceof WildcardType) {
192  9307 return new TypeDescription.Generic.OfWildcardType.ForLoadedType((WildcardType) type, annotationReader);
193    } else {
194  1 throw new IllegalArgumentException("Unknown type: " + type);
195    }
196    }
197   
198    /**
199    * Checks if this type sort represents a non-generic type.
200    *
201    * @return {@code true} if this sort form represents a non-generic.
202    */
 
203  496389 toggle public boolean isNonGeneric() {
204  496389 return this == NON_GENERIC;
205    }
206   
207    /**
208    * Checks if this type sort represents a parameterized type.
209    *
210    * @return {@code true} if this sort form represents a parameterized type.
211    */
 
212  893 toggle public boolean isParameterized() {
213  893 return this == PARAMETERIZED;
214    }
215   
216    /**
217    * Checks if this type sort represents a generic array.
218    *
219    * @return {@code true} if this type sort represents a generic array.
220    */
 
221  39 toggle public boolean isGenericArray() {
222  39 return this == GENERIC_ARRAY;
223    }
224   
225    /**
226    * Checks if this type sort represents a wildcard.
227    *
228    * @return {@code true} if this type sort represents a wildcard.
229    */
 
230  51785 toggle public boolean isWildcard() {
231  51785 return this == WILDCARD;
232    }
233   
234    /**
235    * Checks if this type sort represents a type variable of any form.
236    *
237    * @return {@code true} if this type sort represents an attached type variable.
238    */
 
239  1076 toggle public boolean isTypeVariable() {
240  1076 return this == VARIABLE || this == VARIABLE_SYMBOLIC;
241    }
242   
 
243  6 toggle @Override
244    public String toString() {
245  6 return "TypeDefinition.Sort." + name();
246    }
247    }
248   
249    /**
250    * An iterator that iterates over a type's class hierarchy.
251    */
 
252    class SuperClassIterator implements Iterator<TypeDefinition> {
253   
254    /**
255    * The next class to represent.
256    */
257    private TypeDefinition nextClass;
258   
259    /**
260    * Creates a new iterator.
261    *
262    * @param initialType The initial type of this iterator.
263    */
 
264  387 toggle public SuperClassIterator(TypeDefinition initialType) {
265  387 nextClass = initialType;
266    }
267   
 
268  1075 toggle @Override
269    public boolean hasNext() {
270  1075 return nextClass != null;
271    }
272   
 
273  524 toggle @Override
274    public TypeDefinition next() {
275  524 if (!hasNext()) {
276  5 throw new NoSuchElementException("End of type hierarchy");
277    }
278  519 try {
279  519 return nextClass;
280    } finally {
281  519 nextClass = nextClass.getSuperClass();
282    }
283    }
284   
 
285  1 toggle @Override
286    public void remove() {
287  1 throw new UnsupportedOperationException("remove");
288    }
289   
 
290  3 toggle @Override
291    public String toString() {
292  3 return "TypeDefinition.SuperClassIterator{" +
293    "nextClass=" + nextClass +
294    '}';
295    }
296    }
297    }